﻿using System.Collections.Generic;
using System.Windows;
using Inovout.Gui.Wpf.ViewModels;
using Inovout.Business;
using Inovout.Descriptors;
using Inovout.Portal.Desktop.Workbenchs;

namespace Inovout.Portal.Desktop.Controls
{
    public class RibbonTabViewModel : BaseViewModel
    {
        private FunctionGroup functionGroup;
        private RibbonWorkbenchViewModel workbenchViewModel;

        private Function[] functions;
        internal Function[] Functions
        {
            get 
            {
                if (functions == null)
                {
                    List<Function> functionList = new List<Function>();
                    foreach (var group in Groups)
                    {
                        functionList.AddRange(group.Functions);
                    }
                    functions = functionList.ToArray();
                }
                return functions;
            }
        }

        public RibbonTabViewModel(RibbonWorkbenchViewModel workbenchViewModel, FunctionGroup functionGroup)
        {
            this.workbenchViewModel = workbenchViewModel;
            this.functionGroup = functionGroup;
        }
        internal BaseWorkbenchViewModel WorkbenchViewModel
        {
            get { return workbenchViewModel; }
        }
        public string Header
        {
            get
            {
                ClassDescriptor functionGroupClassDescriptor = null;

                if (functionGroup.GetType().Equals(typeof(FunctionGroup)))
                {
                    functionGroupClassDescriptor = ClassDescriptor.Create(functionGroup.Parent.GetType());
                    return functionGroupClassDescriptor.GetField(functionGroup.Name).Alias;
                }
                else
                {
                    functionGroupClassDescriptor = ClassDescriptor.Create(functionGroup.GetType());
                    return functionGroupClassDescriptor.Alias;
                }
            }
        }

        public Visibility Visibility
        {
            get
            {
                return Groups.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
                //return Groups.Count == 0 ? Visibility.Collapsed
                //    : (Groups.All(group => group.Commands.Any(command => command.IsEnabled)) ? Visibility.Visible : Visibility.Hidden);

            }
        }
        private bool isSelected;
        public bool IsSelected
        {
            get { return isSelected; }
            set 
            {
                if (isSelected != value)
                {
                    isSelected = value;
                    OnPropertyChanged(()=>this.IsSelected);
                }    
            }
        }

        private List<RibbonGroupViewModel> groups;
        public List<RibbonGroupViewModel> Groups
        {
            get
            {
                if (groups == null)
                {
                    groups = new List<RibbonGroupViewModel>();

                    foreach (IFunction f in functionGroup.Functions)
                    {
                        if (f is Function)
                        {
                            if (groups.Count == 0)
                            {
                                RibbonGroupViewModel group = new RibbonGroupViewModel(this, functionGroup);
                                groups.Add(group);
                            }
                        }
                        else
                        {
                            groups.Add(new RibbonGroupViewModel(this, f as FunctionGroup));
                        }
                    }
                }
                return groups;
            }
            set { groups = value; }
        }
    }
}
